;
var helper = require("../../javascript/helper");
var reqeust = require("request");
var db = require("../../javascript/mysql");

var mysql = new db.mysqlClient("accident");
function genProductInfo(params) {
    var projects = [];
    plan = {
        "year": ["0-17"],
        "days": ["1y"],
        "people_count": ["1"],
        "money": ["100000", "200000"],
        "socialSec": ["Y", "N"]
    };

    data = helper.product([plan["year"], plan["days"], plan["people_count"], plan["money"], plan["socialSec"]]);
    for (i = 0; i < data.length; i++) {
        dt = helper.formatProductInfo(data[i], plan);
        projects.push(dt)

    };
    return projects;

}
function httpRequest( dt ) {
    if(dt["money"] == "100000"){
        planCd = "100";
    }else{
        planCd = "200"
    }
    params = {
        "gender": "M",
        "benLevel": "05",
        "coverageAmount": dt["money"],
        "personAge": "10",
        "coverageUnits": planCd,
        "socialSecurity": dt["socialSec"]
    };
    return new Promise(function (resolve, reject) {
        reqeust({
            url: url,
            method: "POST",
            json: false,
            body: helper.urlencode(params),
            headers: {
                "Accept": "application/json, text/javascript, */*; q=0.01",
                "Accept-Language": "zh-CN,zh;q=0.9",
                "Connection": "keep-alive",
                "Content-Type": "application/x-www-form-urlencoded",
                "Cookie": "WEBTRENDS_ID=123.191.244.104-350486736.30781234; cto_lwid=94c7a53c-6c9b-4bc6-a836-71f768e4575c; _ga=GA1.2.1193348258.1575965789; USER_TRACKING_COOKIE=0; Hm_lvt_d06f8617511c35d7eaaa23e187cd568e=1575965782,1575967902; ARK_ID=JSbbfc3a6fa8d46c672b563f9f8b7f0604bbfc; UM_distinctid=16f69d876bbc7-0900b771a5c67-6701434-1fa400-16f69d876bc43c; paid_test=d26103d0-a1d1-3af8-c228-63ca71f38a99; PA_GXH_NSS=; PA_GXH_WSS=; WT-FPC=id=123.191.244.104-350486736.30781234:lv=1587709963385:ss=1587709643712:fs=1575965782313:pn=2:vn=43; adms_location=%E5%85%B6%E4%BB%96|0000000000000000$ALL$ALL$ALL; _gid=GA1.2.1669196535.1589874555; Hm_lvt_2f53c35010dbe120000b9a32bd028225=1587707781,1588142878,1589263852,1589874555; TOA_CSHI_WLS_HTTP_BRIDGE=SpAr6lDgxXcKM7nO3hpE1nu2MMYGbFNHTc_yB-H9uk9-IMoAao8w!-701921883; BIGipServerEHIS-CSHI_PRDPool=486152620.27006.0000; BIGipServerng_pa18-paweb_DMZCLOUD_PrdPool=3445889452.31614.0000; Hm_lvt_5c327520340e087bbe22ba0399dc4cf0=1589246442,1589260695,1589771326,1589874679; Hm_lpvt_5c327520340e087bbe22ba0399dc4cf0=1589874679; route=d0929ca5bf896e0636e041075ede92b0",
                "Sec-Fetch-Dest": "empty",
                "Sec-Fetch-Mode": "cors",
                "Sec-Fetch-Site": "same-origin",
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36",
                "X-Request-Id": "150;r=75382274",
                "X-Requested-With": "XMLHttpRequest"

            }
        }, function (err, res, body) {
            if (!err && res.statusCode == 200) {
                if(body.indexOf("totalPrem")!=-1){
                    price = JSON.parse( body )["totalPrem"].toFixed(2).toString();
                    rs = {
                        "name":"i康保·少儿意外",
                        "company":"平安保险",
                        "price":price
                    }
                    resolve( rs )
                }
            }

        })
    })

}
var url = "https://health.pingan.com/cshi-internet/iKang/ikbseyw/premiumCalculation.action";
var main = async function () {
    data = genProductInfo();
    for(i=0;i<data.length;i++){
        dt = data[i]
        res = await httpRequest( dt );
        result = Object.assign( dt,res );
        mysql.insertData( result );
    }
    mysql.close();
   

}
main();